import { WeaSearchGroup, WeaFormItem, WeaUpload } from "ecCom";
import { Button, Tooltip } from "antd";
import { toJS } from "mobx";
import { WeaSwitch } from "comsMobx";
import { inject, observer } from "mobx-react";

@inject("topStore")
@observer
export default class DialogTask extends React.Component {
  constructor(props) {
    super(props);
  }
  componentDidMount() {

  }
  componentDidUpdate(){
    const { topStore } = this.props;
    topStore.getuploadTaskRef(this.refs);//在组件最后更新的时候给refs 
  }
  render() {
    const { condition } = this.props.listStoreB.listStoreB;
    const { formB,  col } = this.props;
    const { isFormInit } = formB
    let suffix;//标识。防止上传组件数据错乱
    let group = [];
    isFormInit &&
      toJS(condition).map((c, index) => {
        let items = [];
        c.items = c.items || [];
        c.items.map((field, index) => {
          suffix = field.domkey[0];
          items.push({
            com: (
              <div className="g-top-task">
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@l9kxln@${suffix}`}
                  label={`${field.label}`}
                  labelCol={{ span: `${field.labelcol}` }}
                  error={(field.viewAttr==3&&formB.getError(field))}
                  tipPosition="bottom"
                  wrapperCol={{ span: `${field.fieldcol}` }}       
                >
                  {
                    this.getFormItem(field,formB)
                  }
                </WeaFormItem>
              </div>
            ),
            col: col || field.colSpan
          });
          
        });
        group.push(
          <div className="g-top-dialogTask" key={suffix}>
            <div className="number">
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@evyipy@${suffix}`} shape="circle" type="dashed">{index+1}</Button>
            </div>
            <div className="itemBtns">
              <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@b1om06@${suffix}`}
                type="primary"
                shape="circle"
                icon="plus"
                onClick={this.btnAddClick.bind(this, index)}
              />
             {/*  <Tooltip
                title="已是最后一项"
                visible={visibleTip}
                mouseLeaveDelay={0.1}
              > */}
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@2sdg5j@${suffix}`}
                  type="danger"
                  shape="circle"
                  icon="delete"
                  onClick={this.btnDelClick.bind(this, index)}
                  //onMouseLeave={this.btnMouseLeave.bind(this, index)}
                />
              {/* </Tooltip> */}
            </div>
            <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@s3cgie@${suffix}`}
              needTigger={true}
              showGroup={c.defaultshow}
              items={items}
              style={{ background: "#F9F9F9" }}
            />
          </div>
        );
      });

    return group;
  }
  btnAddClick(index) {
    const { topStore } = this.props;
    topStore.btnAddClick(index);
  }
  btnDelClick(index) {
    const { topStore } = this.props;
    topStore.btnDelClick(index);
  }
  // btnMouseLeave(index) {
  //   const { topStore } = this.props;
  //   topStore.btnMouseLeave(index);
  // }

  getFormItem(field, formB) {
    let dom;
    const {topStore} = this.props;
    if (field.conditionType == "UPLOAD") {
      dom = this.getUploadItem(field, formB);
    }
    else
      dom = <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@ymbu1k`} fieldConfig={field} form={formB}/>;
    return dom;
  }

   //获取上传组件
  getUploadItem(field, form) {
    const {topStore} = this.props;
    let {uploadSize, uploadProjectRefs, uploadTaskRefs, saverPro, closeModal, updateForm} = topStore;
    let submitFlag = topStore.submitFlag;
    let upload = {};
    return <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@uidukp`}
      ref = {field.domkey[0]}
      uploadUrl={field.uploadUrl}
      category={field.category}
      viewAttr = {field.viewAttr}
      autoUpload={field.otherParams.autoUpload}
      datas ={field.datas}
      multiSelection = {field.multiSelection}
      maxUploadSize={field.otherParams.maxUploadSize}
      getShowListDatas={(listT, listB) => {
        if (listB.length > 0 && !submitFlag) {
          upload[field.domkey] = "xk";
          form.updateFields(upload);//赋值绕过检验规则
        }
        updateForm(listT, listB, upload, form, field, uploadSize);
      } }
      >
    </WeaUpload>
  }
}
